package com.adminsystem.java.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author wangpeiao
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeHttpRequests(authorize -> authorize
                    .antMatchers("/myAdminSystem/user/register").permitAll()
                    .antMatchers("/myAdminSystem/user/login").permitAll()
                    .antMatchers("/myAdminSystem/user/logout").permitAll()
                    .antMatchers("/myAdminSystem/user/current").permitAll()
                    .antMatchers("/myAdminSystem/user/forgot-password").permitAll()
                    .antMatchers("/myAdminSystem/user/reset-password").permitAll()
                    .antMatchers("/myAdminSystem/user/list").permitAll()
                    .antMatchers("/myAdminSystem/user/detail").permitAll()
                    .antMatchers("/myAdminSystem/user/edit").permitAll()
                    .antMatchers("/myAdminSystem/user/delete").permitAll()
                    .antMatchers("/myAdminSystem/user/add").permitAll()
                    .antMatchers("/myAdminSystem/user/change-status").permitAll()
                    .antMatchers("/myAdminSystem/user/deleteByIds").permitAll()
                    .antMatchers("/myAdminSystem/user/download").permitAll()
                    .antMatchers("/myAdminSystem/devlog/list").permitAll()
                    .antMatchers("/myAdminSystem/devlog/add").permitAll()
                    .antMatchers("/myAdminSystem/studentScore/list").permitAll()
                    .antMatchers("/myAdminSystem/studentScore/export").permitAll()
                    .antMatchers("/myAdminSystem/studentScore/template").permitAll()
                    .antMatchers("/myAdminSystem/studentScore/import").permitAll()
                    .anyRequest().authenticated()
            );
        return http.build();
    }

}